package com.sisi.web;

import entity.BanJi813;
import util.JavaUtil;
import util.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/banJi813")
public class BanJi813Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method == null || method.equals("")){
            method = "selectByPage";
        }
        switch (method){
            case "selectByPage" :
                selectByPage(req,resp);
                break;

        }
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("selectByPage");
        String pageNOStr = req.getParameter("pageNo");
        if (pageNOStr == null ||pageNOStr.equals("")){
            pageNOStr = "1";
        }
        int pageNO = Integer.parseInt(pageNOStr);
        String pageSizeStr = req.getParameter("pageSize");
        if (pageSizeStr == null ||pageSizeStr.equals("")){
            pageSizeStr = "5";
        }
        int pageSize = Integer.parseInt(pageSizeStr);
        int offset = (pageNO - 1) * pageSize;//从什么位置开始偏移
        ArrayList<BanJi813> list = getCurrentPage(offset,pageSize);//当前页的数据集合
        int totalCount = getTotalCount();//得到总的数量
        int totalPage = (int)Math.ceil((double) totalCount / pageSize);//得到所有页的数量
        PageInfo pageInfo = new PageInfo(list,pageNO,totalPage,pageSize);
        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("/banJi_list.jsp").forward(req,resp);
    }

    private int getTotalCount() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int totalCount = 0;
        try {
            connection = JavaUtil.getConnection();
            String sql = "SELECT count(*) FROM banJi813";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            if (resultSet.next()){
                totalCount = resultSet.getInt(1);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JavaUtil.close(connection,statement,resultSet);
        }
        return totalCount;
    }

    private ArrayList<BanJi813> getCurrentPage(int offset, int pageSize) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        ArrayList<BanJi813> arrayList = new ArrayList<>();
        try {
            connection = JavaUtil.getConnection();
            String sql = "SELECT id,name,address FROM banJi813 LIMIT ?,?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,offset);
            statement.setInt(2,pageSize);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String address = resultSet.getString("address");
                BanJi813 banJi813 = new BanJi813(id,name,address);
                arrayList.add(banJi813);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JavaUtil.close(connection,statement,resultSet);
        }
        return arrayList;
    }
}
